package com.wushi.lenovo.asia5b.app_ui.other.dialog.trans;

import android.view.View;

import com.mvp.base.model.ResultModel;
import com.mvp.utils.DecimalUtil;
import com.mvp.utils.StringUtils;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_event.WithdrawEvent;
import com.wushi.lenovo.asia5b.app_model.credit.MemberBlanceModel;
import com.wushi.lenovo.asia5b.app_model.other.TransItemBean;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * Created by zhangyuncai on 2019/11/27.
 * 提现
 */
public class WithdrawDFragment extends PayDialogFragment {
    Map<String, Object> map;
    MemberBlanceModel memberBlanceModel;
    String account;

    public void setData(Map<String, Object> map, MemberBlanceModel memberBlanceModel, String account) {
        this.map = map;
        this.memberBlanceModel = memberBlanceModel;
        this.account = account;
    }

    @Override
    protected void initView() {
        super.initView();
        setViewForWithdraw();
    }

    private void setViewForWithdraw() {
        mDatas.clear();
        mDatas.add(new TransItemBean(getString(R.string.withdraw_recipientbank), map.get("holeded_bank").toString()));//账户金额
        mDatas.add(new TransItemBean(getString(R.string.withdraw_accountnumber), map.get("bank_acc_no").toString()));//账户金额
        mDatas.add(new TransItemBean(getString(R.string.withdraw_recipientname), map.get("account_holder_name").toString()));//账户金额
        mDatas.add(new TransItemBean(getString(R.string.withdraw_creditbalance), memberBlanceModel.getBalance_value()));//账户金额
        mDatas.add(new TransItemBean(getString(R.string.withdraw_withdrawamount), account + ""));//账户金额
        String totalBalance = DecimalUtil.subtract(memberBlanceModel.getBalance_value(), account);
        mDatas.add(new TransItemBean(getString(R.string.withdraw_totalbalance), StringUtils.formatNum2(totalBalance)));//账户金额
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 提现的接口
     *
     * @param payPass
     * @return
     */
    @Override
    protected Observable<ResultModel> getSubmitObservable(String payPass) {
        map.put("payment_password", payPass);
        return apiService.getSubmitWithdrawData(map);

    }

    /**
     * 提现成功
     *
     * @return
     */
    @Override
    protected Consumer<ResultModel> getSubmitResult() {
        return resultModel -> {
            if (resultModel.isSuccess()) {
                EventBus.getDefault().post(new WithdrawEvent());
            }
            int successText = R.string.paymentsuccessful;
            int errorText = R.string.paymentfailed;
            ll_trans_notice_vessel.setVisibility(View.VISIBLE);
            if (resultModel.isSuccess()) {
                iv_trans_noticeicon.setImageResource(R.drawable.icon_trans_right);
                tv_trans_notice.setText(successText);
            } else {
                iv_trans_noticeicon.setImageResource(R.drawable.icon_trans_error);
                tv_trans_notice.setText(errorText);
            }
        };
    }

}
